import { request } from "@umijs/max";
import requestTable from "@/utils/requestTable";
import type { TYPE } from "../types";

/**
 * 获取瓶子数据
 */
function bottle(params: TYPE.BottleItem) {
  // 字符串转数值，例如："0" => 0
  const { status } = params;
  if (status) params.status = status - 0;
  return requestTable("bottle", params);
}
/**
 * 新增瓶子数据
 */
function bottleAdd(data: TYPE.BottleItem) {
  return request("bottle/add", { method: "post", data });
}
/**
 * 编辑瓶子数据
 */
function bottleEdit(data: TYPE.BottleItem) {
  return request("bottle/edit", { method: "post", data });
}
/**
 * 删除瓶子数据
 */
function bottleRemove(id: number) {
  return request("bottle/remove", { params: { ids: id } });
}

/**
 * 上传瓶子照片
 */
function bottleUpload(ext: string, b64 = "", b64thumb = "") {
  const data: any = { ext };
  const skipErrorHandler = true;
  if (ext === "jpg") {
    data.b64 = b64;
    data.b64thumb = b64thumb;
  }
  return request("bottle/upload", { method: "post", data, skipErrorHandler });
}
/**
 * 删除瓶子照片
 */
function bottleRemoveFile(filePath: string) {
  return request("bottle/removeFile", { params: { filePath } });
}

/**
 * 新瓶入库
 */
function stockIn(id: number, num: number) {
  return request("bottle/stockIn", { params: { id, num } });
}

/**
 * 上架
 */
function takeUp(id: number, num: number) {
  return request("bottle/takeUp", { params: { id, num } });
}
/**
 * 售出
 */
function dispose(id: number, num: number) {
  return request("bottle/dispose", { params: { id, num } });
}
/**
 * 下架返库
 */
function takeDown(id: number, num: number) {
  return request("bottle/takeDown", { params: { id, num } });
}
/**
 * 丢弃
 */
function abandon(id: number, num: number) {
  return request("bottle/abandon", { params: { id, num } });
}

export default {
  bottle, bottleAdd, bottleEdit, bottleRemove,
  bottleUpload, bottleRemoveFile,
  stockIn, takeUp, dispose, takeDown, abandon,
};
